package com.example.mannortycoon.controller;

import com.example.mannortycoon.entity.SeedEntity;
import com.example.mannortycoon.entity.SeedUnlockConditionsEntity;
import com.example.mannortycoon.service.SeedService;
import com.example.mannortycoon.service.SeedUnlockConditionsService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.SeedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RequestMapping("/seed")
@RestController
@Api(tags="种子")
public class SeedController {

    private final SeedService seedService;

    private final SeedUnlockConditionsService seedUnlockConditionsService;

    @Autowired
    public SeedController(SeedService seedService,SeedUnlockConditionsService seedUnlockConditionsService){
        this.seedService = seedService;
        this.seedUnlockConditionsService = seedUnlockConditionsService;
    }

    @GetMapping("/getSeedList")
    @ApiOperation(value = "商城获取种子列表")
    public R getSeedList(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        List<SeedVo> list = seedService.getSeedList(userId);
        return R.ok().put("data",list);
    }

    @GetMapping("/getSeedOptions")
    public R getSeedOptions(){
        List<SeedEntity> list = seedService.list();
        return R.ok().put("data",list);
    }


    @GetMapping("/list")
    @ApiIgnore
    public R list(@RequestParam Map<String,Object> params){
        PageUtils page = seedService.queryPage(params);
        return R.ok().put("page",page);
    }



    @GetMapping("/info/{seedId}")
    @ApiOperation(value = "获取种子信息")
    @ApiIgnore
    public R info(@PathVariable("seedId") Long seedId){
        SeedEntity seedEntity = seedService.getById(seedId);
        List<SeedUnlockConditionsEntity> unlockConditions= seedUnlockConditionsService.listBySeedId(seedId);
        seedEntity.setUnlockConditions(unlockConditions);
        return R.ok().put("data", seedEntity);
    }

    @PostMapping("/save")
    @ApiIgnore
    public R save(@RequestBody SeedEntity seedEntity){
        List<SeedUnlockConditionsEntity> unlockConditions = seedEntity.getUnlockConditions();
        seedUnlockConditionsService.save(unlockConditions);
        seedEntity.setCreateTime(new Date());
        seedEntity.setUpdateTime(new Date());
        //新增种子同时新增作物
        seedService.saveSeed(seedEntity);
        return R.ok();
    }

    @PutMapping("/update")
    @ApiIgnore
    public R update(@RequestBody SeedEntity seedEntity){
        seedUnlockConditionsService.update(seedEntity.getUnlockConditions(),seedEntity.getSeedId());
        seedEntity.setUpdateTime(new Date());
        seedService.updateById(seedEntity);
        return R.ok();
    }

    @DeleteMapping("/delete")
    @ApiIgnore
    public R delete(@RequestBody Long[] seedIds){
        seedService.removeByIds(Arrays.asList(seedIds));
        return R.ok();
    }

}
